package com.jmes.web.view;

import com.jmes.web.filter.AuthFilter;
import com.jmes.web.model.FunctionCodeTable;
import com.jmes.web.model.FunctionTreeNode;
import com.jmes.web.model.User;
import com.jmes.web.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

/**
 * Home首页.
 * User: jinshunlong
 * Date: 13-4-14
 * Time: 上午12:37
 */
public class HomeComposer implements Composer {

    private static Logger logger = Logger.getLogger(HomeComposer.class);
    /**
     * Invokes after ZK loader creates this component,
     * initializes it and composes all its children, if any.
     *
     * @param comp the component has been composed
     */
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        doLogout(comp);
        doFunctionTree(comp);
    }

    /**
     * 处理退出事件
     * @param comp
     * @throws Exception
     */
    public void doLogout(Component comp)throws Exception{
        final Button button = (Button)comp.getFellow("logoutButton");
        button.addEventListener("onClick",new EventListener<Event>() {
            @Override
            public void onEvent(Event event) throws Exception {
                AuthFilter.logout((HttpSession)button.getDesktop().getSession().getNativeSession());
                Executions.sendRedirect("/login.zul");
            }
        });
    }

    /**
     * 处理功能区域
     * @param comp
     * @throws Exception
     */
    public void doFunctionTree(final Component comp)throws Exception{
        Treechildren treechildren = (Treechildren)comp.getFellow("functionTreeContent");
        User user = AuthFilter.currentUser(comp);

        List<FunctionTreeNode> rootFunctions = RoleService.findUserFunctionTreeByRoleId(user.getRoleId());
        parseTreeFunction(rootFunctions,comp,treechildren);
    }
    private void parseTreeFunction(Collection<FunctionTreeNode> nodes,final Component comp,Component parentTreeItem){
        for(FunctionTreeNode node: nodes){
            logger.info("===" + node.getFunctionCodeTable().getId());
            Treeitem treeitem = new Treeitem();
            FunctionCodeTable functionCodeTable = node.getFunctionCodeTable();
            treeitem.setLabel(functionCodeTable.getName());
            if(!StringUtils.isBlank(functionCodeTable.getUrl())){
                treeitem.setAttribute("_url",functionCodeTable.getUrl());
                treeitem.addEventListener("onClick", new EventListener<Event>() {
                    @Override
                    public void onEvent(Event event) throws Exception {
                        Treeitem treeitem = (Treeitem) event.getTarget();
                        String itemName = treeitem.getLabel();
                        String url = (String) treeitem.getAttribute("_url");
                        Tabbox tabbox = (Tabbox) comp.getFellow("functionTab");
                        Tabs tabs = tabbox.getTabs();
                        List<Component> tabList = tabs.getChildren();
                        for (Component tabComp : tabList) {
                            Tab oneTab = (Tab) tabComp;
                            if (itemName.equals(oneTab.getLabel())) {
                                oneTab.setSelected(true);
                                return;
                            }
                        }
                        Tab newTab = new Tab();
                        newTab.setLabel(itemName);
                        newTab.setClosable(true);
                        tabs.appendChild(newTab);

                        Iframe iframe = new Iframe();
                        iframe.setSrc(url);
                        iframe.setWidth("100%");
                        iframe.setHeight("100%");
                        iframe.setStyle("border: 0px solid gray");
                        Tabpanel newTabPanel = new Tabpanel();
                        newTabPanel.appendChild(iframe);
                        tabbox.getTabpanels().appendChild(newTabPanel);

                    }
                });
            }
            if(node.getChildFunction().size() != 0){
                Treechildren treechildren = new Treechildren();
                treeitem.appendChild(treechildren);
                parseTreeFunction(node.getChildFunction().values(), comp, treechildren);
            }

            parentTreeItem.appendChild(treeitem);
        }
    }
}
